import { defineStore } from "pinia";

export const useWebStore = defineStore("web", {
  state: () => {
    return {
      bannerHeight: 0,
      headerShadowActive: false,
      headerShow: false,
      headerLogoShow: true,
      navDarkActive: false,
      isDynamicAddedRoute: false,
      isCompanyInfoObtain: false,
      companyInfo: "",
      isNavListObtain: false,
      navList: "",
      loginState: false,
      userInfo: null,
      publishTitle: "",
      refreshRecentMeeting: false,
      currentModel: "",
      lastVisitRoute: ""
    };
  },
  getters: {
    getIsDynamicAddedRoute(state): boolean {
      return state.isDynamicAddedRoute;
    },
    getIsCompanyInfoObtain(state): boolean {
      return state.isCompanyInfoObtain;
    },
    getCompanyInfo(state): object {
      return state.companyInfo ? JSON.parse(state.companyInfo) : "";
    },
    getIsNavListObtain(state): boolean {
      return state.isNavListObtain;
    },
    getNavList(state): object {
      return state.navList ? JSON.parse(state.navList) : "";
    },
    getLoginState(state): boolean {
      return state.loginState || localStorage.getItem("loginState") == "true";
    },
    getUserInfo(state): object {
      return state.userInfo || !!localStorage.getItem("userInfo") ? JSON.parse(localStorage.getItem("userInfo") || "") : {};
    },
    getPublishTitle(state): string {
      if (!!state?.publishTitle) {
        return state.publishTitle;
      } else {
        return localStorage?.getItem("publishTitle") ?? "";
      }
    },
    getRefreshRecentMeeting(state): boolean {
      return state.refreshRecentMeeting;
    },
    getLastVisitRoute(state): string {
      return state.lastVisitRoute;
    }
  },
  actions: {
    setBannerHeight(value) {
      this.bannerHeight = value.bannerHeight;
    },
    setShadowActive(value) {
      this.headerShadowActive = value.headerShadowActive;
    },
    setHeaderShow(value) {
      this.headerShow = value.headerShow;
    },
    setHeaderLogo(value) {
      this.headerLogoShow = value.headerLogoShow;
    },
    setNavDarkActive(value) {
      this.navDarkActive = value.navDarkActive;
    },
    setDynamicAddedRoute(value) {
      this.isDynamicAddedRoute = value.isDynamicAddedRoute;
    },
    setCompanyInfoObtain(value) {
      this.isCompanyInfoObtain = value.isCompanyInfoObtain;
    },
    setCompanyInfo(value) {
      this.companyInfo = JSON.stringify(value.companyInfo);
    },
    setNavListObtain(value) {
      this.isNavListObtain = value.isNavListObtain;
    },
    setNavList(value) {
      this.navList = JSON.stringify(value.navList);
    },
    setPublishTitle(value) {
      this.publishTitle = value.publishTitle;
      localStorage.setItem("publishTitle", value.publishTitle);
    },
    setCurrentModel(value) {
      this.currentModel = value.path;
    },
    setLastVisitRoute(value) {
      this.lastVisitRoute = value.lastVisitRoute;
    },
    userLogin(userInfo) {
      this.loginState = true;
      this.userInfo = userInfo;
      localStorage.setItem("loginState", "true");
      localStorage.setItem("userInfo", JSON.stringify(userInfo));
    },
    userLogout() {
      this.loginState = false;
      // @ts-ignore
      this.userInfo = {};
      localStorage.removeItem("userInfo");
      localStorage.removeItem("loginState");
      localStorage.removeItem("token");
    },
    setRefreshRecentMeeting(value) {
      this.refreshRecentMeeting = value.refreshRecentMeeting;
    },
    setUserInfo(val) {
      localStorage.setItem("userInfo", JSON.stringify(val));
      return this.userInfo = val;
    }
  }
});
